package com.software.notification.dialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.software.notification.R;

import java.util.Calendar;

public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener,
        DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {

    private Button btn_exit;
    private TextView tv_date, tv_time;
    private Button btn_date, btn_time;
    private Button btn_custom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog);
        initViews();
        initEvents();
    }

    private void initEvents() {
        btn_exit.setOnClickListener(this);
        btn_date.setOnClickListener(this);
        btn_time.setOnClickListener(this);
        btn_custom.setOnClickListener(this);
    }

    private void initViews() {
        btn_exit = findViewById(R.id.btn_exit);
        btn_date = findViewById(R.id.btn_date);
        btn_time = findViewById(R.id.btn_time);
        tv_date = findViewById(R.id.tv_date);
        tv_time = findViewById(R.id.tv_time);

        btn_custom = findViewById(R.id.btn_custom);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_exit){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setIcon(R.mipmap.msg);
            builder.setTitle("提示");
            builder.setMessage("确定不退出吗？");
            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    //关闭
                    AlertDialogActivity.this.finish();

                }
            });
            builder.setCancelable(false);
            builder.setNegativeButton("确定", null);
            Dialog dialog = builder.create();
            dialog.show();
        } else if (id == R.id.btn_date) {
            Calendar calendar = Calendar.getInstance();
            DatePickerDialog dialog = new DatePickerDialog(
                    this,
                    this,
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH)
            );
            dialog.show();
        }else if (id == R.id.btn_time) {
            Calendar calendar = Calendar.getInstance();
            TimePickerDialog dialog = new TimePickerDialog(
                    this,
                    this,
                    calendar.get(Calendar.HOUR),
                    calendar.get(Calendar.MINUTE),
                    true
            );
            dialog.show();
        }else if (id == R.id.btn_custom) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setIcon(R.mipmap.msg);
            builder.setTitle("自定义对话框");
            //获取布局文件  这里不能设置message
            View view = getLayoutInflater().inflate(R.layout.dialog_custom, null);
            //给自定义的视图控件绑定事件， 需要使用view.findViewById()
            Button btn_ok = view.findViewById(R.id.btn_ok);
            btn_ok.setOnClickListener(w -> {
                TextView tv_work = view.findViewById(R.id.tv_work);
                tv_work.setText("dark");
            });

            builder.setView(view);
            //按钮
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    AlertDialogActivity.this.finish();
                }
            });
            builder.setNegativeButton("取消", null);
            Dialog dialog = builder.create();
            dialog.show();
        }
    }

    /**
     * 日期选中后需要做什么
     * @param view the picker associated with the dialog
     * @param year the selected year
     * @param month the selected month (0-11 for compatibility with
     *              {@link Calendar#MONTH})
     * @param dayOfMonth the selected day of the month (1-31, depending on
     *                   month)
     */
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        //放到文本框中
        String desc = String.format("你选择的日期是：%d年%d月%d日", year, month + 1, dayOfMonth);
        tv_date.setText(desc);



    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String desc = String.format("你选择的时间是：%d时%d分", hourOfDay, minute);
        tv_time.setText(desc);
    }
}